import { invoke } from '@tauri-apps/api/core';
import { TauriApiResponse } from '@/types/api';

export const settingsService = {
  async get<T = any>(key: string): Promise<TauriApiResponse<T>> {
    try {
      return await invoke('settings_get', { key });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to get setting',
      };
    }
  },

  async set(key: string, value: any): Promise<TauriApiResponse<void>> {
    try {
      return await invoke('settings_set', { key, value });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to set setting',
      };
    }
  },

  async remove(key: string): Promise<TauriApiResponse<void>> {
    try {
      return await invoke('settings_remove', { key });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to remove setting',
      };
    }
  },

  async clear(): Promise<TauriApiResponse<void>> {
    try {
      return await invoke('settings_clear');
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to clear settings',
      };
    }
  },

  async getAll(): Promise<TauriApiResponse<Record<string, any>>> {
    try {
      return await invoke('settings_get_all');
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to get all settings',
      };
    }
  },

  async export(path: string): Promise<TauriApiResponse<void>> {
    try {
      return await invoke('settings_export', { path });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to export settings',
      };
    }
  },

  async import(path: string): Promise<TauriApiResponse<void>> {
    try {
      return await invoke('settings_import', { path });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to import settings',
      };
    }
  },
};
